﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SGArchiveSystem7x.App_Code;

namespace SGArchiveSystem7x.Controllers.User
{
    public class RoleController : BaseController
    {
        /// <summary>
        /// 本控制器的名称，用于权限访问 (档案权限)
        /// </summary>
        private string sControllerName = "Role";
        // GET: Role

        public ActionResult Index()
        {
            var n = this.VaildUser(sControllerName);
            if (n != null)
                return n;

            ///清除必要的SESSION
            CSession.Clear_PageSession(Session);

            if(Models.Language._nowLanguage == SGArchiveSystem7xDB._VersionLanguage._Chs)
            {
                ViewBag.Text01 = "角色名称:";
                ViewBag.Text02 = "是否启用:";
                ViewBag.Text1 = "查询";
                ViewBag.Text2 = "添加角色";
                ViewBag.Text3 = "编辑";
                ViewBag.Text4 = "禁止";
                ViewBag.Text5 = "启用";
                ViewBag.Text6 = "角色信息";
            }
            else
            {
                ViewBag.Text01 = "Role Name:";
                ViewBag.Text02 = "Role Status:";
                ViewBag.Text1 = "Search";
                ViewBag.Text2 = "Add Role";
                ViewBag.Text3 = "Edit";
                ViewBag.Text4 = "Stop";
                ViewBag.Text5 = "Enable";
                ViewBag.Text6 = "Role Info";
            }

            ///角色grid 的列
            var columns = SGArchiveSystem7x.Models.CColumnsModel.Create_RoleListColumns();
            ViewBag.Columns = columns;

            var mj = SGArchiveSystem7x.Models.CComboboxModel.Create_ComboboxMJ(false);
            ViewBag.MJ = mj;
            return View();
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxRoleList(string name, int status)
        {
            int count = 0;
            var listWhere = new List<string>();
            listWhere.Add(status.ToString());
            listWhere.Add(name);
            CSession.Add_QueryWhere(Session, listWhere);
            var list = SGArchiveSystem7xDB.Linq.T_RoleDal.Get_Role(out count, status, name, 0, CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = list.Count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取角色列表分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_ajaxRoleListPager(int num, int size)
        {
            ///分页后的起始行
            int iStart = (num - 1) * size;
            var listWhere = CSession.Get_QueryWhere(Session);
            int count = 0;
            var list = SGArchiveSystem7xDB.Linq.T_RoleDal.Get_Role(out count, int.Parse(listWhere[0]), listWhere[1], iStart, CWebCommons._iPageRows);
            var json = new
            {
                rows = list.ToArray(),
                total = list.Count
            };
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 角色信息
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleInfo(int id)
        {
            if (Models.Language._nowLanguage == SGArchiveSystem7xDB._VersionLanguage._Chs)
            {
                ViewBag.Text0 = "角色列表";
                ViewBag.Text1 = "角色名称:";
                ViewBag.Text2 = "角色备注:";
                ViewBag.Text3 = "是否启用:";
                ViewBag.Text4 = "提示";
                ViewBag.Text5 = "确定要保存角色信息吗?";
                ViewBag.Text6 = "必须输入角色名称!";
                ViewBag.Text7 = "保存失败!";
                ViewBag.Text8 = "保存成功!";
                ViewBag.Text9 = "保存";
            }
            else
            {
                ViewBag.Text0 = "Role List";
                ViewBag.Text1 = "Role Name:";
                ViewBag.Text2 = "Role Remark:";
                ViewBag.Text3 = "Status:";
                ViewBag.Text4 = "Tip";
                ViewBag.Text5 = "Are you sure you want to save role?";
                ViewBag.Text6 = "Role Name cannot be empty";
                ViewBag.Text7 = "Save failed";
                ViewBag.Text8 = "Save successfully";
                ViewBag.Text9 = "Save";
            }

            ViewBag.ID = id;
            var listModel = Models.CComboboxModel.Create_ComboboxMainModel(false);
            ViewBag.Model = listModel;

            var listMJ = Models.CComboboxModel.Create_ComboboxMJ(false);
            ViewBag.MJ = listMJ;

            if(id == 0)
            {
                SGArchiveSystem7xDB.T_Role en = new SGArchiveSystem7xDB.T_Role();
                ViewBag.ROLE = en;
            }
            else
            {
                SGArchiveSystem7xDB.T_Role en = SGArchiveSystem7xDB.Linq.T_RoleDal.Get_Role(id);
                ViewBag.ROLE = en;
            }

            return View();
        }


        /// <summary>
        /// 添加或修改角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="remark"></param>
        /// <param name="status"></param>
        /// <param name="au"></param>
        /// <returns></returns>
        [HttpPost]
        public string SaveRole(int id, string name, string remark, int status, string au, int mj, int space)
        {
            var user = SGArchiveSystem7x.App_Code.CSession.Get_User(Session);

            if (au.EndsWith(","))
                au = au.Substring(0, au.LastIndexOf(","));
            SGArchiveSystem7xDB.T_Role en = new SGArchiveSystem7xDB.T_Role();
            en.ROLE_ID = id;
            en.ROLE_NAME = name;
            en.ROLE_REAMARK = remark;
            en.ROLE_STATUS = status;
            en.ROLE_V01 = au;
            en.ROLE_LASTDATE = DateTime.Now;
            en.ROLE_LASTUSERNAME = user.U_RNAME;
            en.ROLE_MJ = mj;
            en.ROLE_I01 = space;
            if (id == 0) 
                return ((int)SGArchiveSystem7xDB.Linq.T_RoleDal.Add_Role(en)).ToString();
            else
            {
                int value = (int)SGArchiveSystem7xDB.Linq.T_RoleDal.Modify_Role(en);
                return value.ToString();
            }
                ///return ().ToString();
        }
    }
}